﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.IDAL.FoodTesting;
using StoragePeriod.Model.FoodTesting;
using System.Data;
using StoragePeriod.DBUtility;
using System.Data.SqlClient;

namespace StoragePeriod.SqlServerDAL.FoodTesting
{
    /// <summary>
    /// 碳水化合物 接口
    /// </summary>
    public class D_MineralData : I_MineralData
    {
        /// <summary>
        /// 获取检查数据实体
        /// </summary>
        /// <param name="eVcData">查询ID</param>
        /// <returns>返回对应的实体</returns>
        public E_MineralData GetDataEntityById(E_MineralData eMineralData)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select * from MineralData where id=" + eMineralData.id);
            DataTable dt = DbHelperSQL.Query(sbSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                E_MineralData Entity = new ModelHandler<E_MineralData>().FillModel(dt.Rows[0]);
                return Entity;
            }
            return null;
        }

        /// <summary>
        /// 根据原料ID和检测时间 删除检测记录
        /// </summary>
        /// <param name="eVcData">要删除的检测记录</param>
        /// <returns></returns>
        public int RemoveByTime(E_MineralData eMineralData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from MineralData where RawId=" + eMineralData.RawId + " and MineralTime=CAST('" + eMineralData.MineralTime.ToString() + "' as datetime)");
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除某一次检测记录
        /// </summary>
        /// <param name="eVcData">要删除的检测记录</param>
        /// <returns></returns>
        public int RemoveByFoodNum(E_MineralData eMineralData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from MineralData where RawId=" + eMineralData.RawId + " and MineralTime=CAST('" + eMineralData.MineralTime.ToString() + "' as datetime) and FoodNum=" + eMineralData.FoodNum);
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 根据原料ID 和检测时间 获取所有的检测记录
        /// </summary>
        /// <param name="eVcData">要获取的检测记录</param>
        /// <returns></returns>
        public List<E_MineralData> GetDataListByTime(E_MineralData eMineralData)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select * from MineralData where RawId=" + eMineralData.RawId + " and MineralTime=cast('" + eMineralData.MineralTime.ToString() + "' as datetime)");
            DataTable dt = DbHelperSQL.Query(sbSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                List<E_MineralData> EntityList = new ModelHandler<E_MineralData>().FillModel(dt);
                return EntityList;
            }
            return null;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_MineralData eMineralData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into MineralData(");
            strSql.Append("RawId,RawName,Mineral_Zn,Mineral_C1,Mineral_C2,Mineral_V,Mineral_f,Mineral_m,Mineral_Fe,Mineral_Ca,Mineral_K,MineralTime,FoodNum)");
            strSql.Append(" values (");
            strSql.Append("@RawId,@RawName,@Mineral_Zn,@Mineral_C1,@Mineral_C2,@Mineral_V,@Mineral_f,@Mineral_m,@Mineral_Fe,@Mineral_Ca,@Mineral_K,@MineralTime,@FoodNum)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RawId", SqlDbType.Int,4),
					new SqlParameter("@RawName", SqlDbType.VarChar,50),
					new SqlParameter("@Mineral_Zn", SqlDbType.VarChar,50),
					new SqlParameter("@Mineral_C1", SqlDbType.VarChar,50),
					new SqlParameter("@Mineral_C2", SqlDbType.VarChar,50),
					new SqlParameter("@Mineral_V", SqlDbType.VarChar,50),
					new SqlParameter("@Mineral_f", SqlDbType.VarChar,50),
					new SqlParameter("@Mineral_m", SqlDbType.VarChar,50),
					new SqlParameter("@Mineral_Fe", SqlDbType.VarChar,50),
					new SqlParameter("@Mineral_Ca", SqlDbType.VarChar,50),
					new SqlParameter("@Mineral_K", SqlDbType.VarChar,50),
					new SqlParameter("@MineralTime", SqlDbType.DateTime),
					new SqlParameter("@FoodNum", SqlDbType.Int,4)};
            parameters[0].Value = eMineralData.RawId;
            parameters[1].Value = eMineralData.RawName;
            parameters[2].Value = eMineralData.Mineral_Zn;
            parameters[3].Value = eMineralData.Mineral_C1;
            parameters[4].Value = eMineralData.Mineral_C2;
            parameters[5].Value = eMineralData.Mineral_V;
            parameters[6].Value = eMineralData.Mineral_f;
            parameters[7].Value = eMineralData.Mineral_m;
            parameters[8].Value = eMineralData.Mineral_Fe;
            parameters[9].Value = eMineralData.Mineral_Ca;
            parameters[10].Value = eMineralData.Mineral_K;
            parameters[11].Value = eMineralData.MineralTime;
            parameters[12].Value = eMineralData.FoodNum;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}
